package 力扣;

/**
 * @author yyq
 * @create 2022-06-07 8:55
 */
public class leetcode122 {
    public static void main(String[] args) {
        leetcode122 leetcode122=new leetcode122();
        System.out.println(leetcode122.maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
    }
    public int maxProfit(int[] prices) {
        int profit = 0;
        int price = -1;
        if(prices.length==1) return 0;
        for (int i = 0; i < prices.length; i++) {
            if(i==0) {
                if(prices[i+1]>prices[i]) {
                    price=prices[i];
                    continue;
                }
                else continue;
            }


            if(i==prices.length-1){
                if (price==-1) continue;
                else {
                    profit =  profit + prices[i] - price;
                    continue;
                }
            }

            // 买入时机
            if(price==-1&&prices[i]<=prices[i-1]&&prices[i]<prices[i+1]){
                price = prices[i];
            }
            // 卖出时机
            if(price!=-1&&prices[i]>price&&prices[i+1]<prices[i]){
                profit =profit + prices[i] - price;
                price = -1;
            }

        }
        return profit;

    }
}
